package com.xr.miniprogram.controller.comment;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xr.common.core.controller.BaseController;
import com.xr.common.core.domain.AjaxResult;
import com.xr.common.core.page.TableDataInfo;
import com.xr.miniprogram.domain.MiniProgramComment;
import com.xr.miniprogram.domain.MiniProgramOrderUser;
import com.xr.miniprogram.domain.MiniProgramServiceLog;
import com.xr.miniprogram.domain.vo.MiniProgramCommentResultVO;
import com.xr.miniprogram.domain.vo.MiniProgramCommentVO;
import com.xr.miniprogram.service.IMiniProgramCommentService;
import com.xr.miniprogram.service.IMiniProgramOrderUserService;
import com.xr.miniprogram.service.IMiniProgramServiceLogService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/miniprogram/comment")
public class MiniProgramCommentController extends BaseController {
    @Resource
    private IMiniProgramCommentService commentService;
    @Autowired
    private IMiniProgramOrderUserService orderUserService;
    @Autowired
    private IMiniProgramServiceLogService serviceLogService;


    /**
     * 根据服务id查询评价数，好评数，差评数，中评数
     * @param serviceId 服务id
     * @return
     */
    @RequestMapping("/rate/{serviceId}")
    public AjaxResult selectrate(@PathVariable("serviceId") Long serviceId){
        MiniProgramCommentResultVO resultVo = commentService.selectRate(serviceId);
        return success(resultVo);
    }

    /**
     * 根据服务id 分页 分类 查询评论
     * @param serviceId 服务id
     * @param topic 类别
     * @return
     */
    @GetMapping("/sel/{serviceId}/{topic}")
    public TableDataInfo selectByDiscount(@PathVariable("serviceId") Long serviceId, @PathVariable("topic") Long topic){
        startPage();
        TableDataInfo table = getDataTable(commentService.selectByServiceIdAndTopic(serviceId, topic));
        table.setRows(table.getRows().stream().map(comment -> MiniProgramCommentVO.toMiniProgramCommentVO((MiniProgramComment) comment)).collect(Collectors.toList()));
        return table;
    }

    /**
     * 根据orderUserId获取评论
     * @param
     * @return
     */
    @RequestMapping("/select/{orderUserId}")
    public AjaxResult addComment(@PathVariable("orderUserId") Long orderUserId){
        return success(commentService.selectByOrderUserId(orderUserId));
    }

    /**
     * 修改评论(实际上就是用户填写了评论)
     * @param miniProgramComment
     * @return
     */
    @Transactional
    @PostMapping("/update")
    public AjaxResult addComment(@RequestBody MiniProgramComment miniProgramComment){
        miniProgramComment.setCreateTime(new Date());
        commentService.updateById(miniProgramComment);
        //用户填写了评论，该订单用户状态 待评价-->服务完成
        orderUserService.updateStatusForComment(miniProgramComment);
        return success();
    }
}
